package com.lanou.sfu.songforu.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.datas.RecommendSongListDatas;
import com.lanou.sfu.songforu.singleton.VolleySingleton;

import java.util.List;

/**
 * Created by quyan on 15/10/22.
 */
public class RecommendSongListAdapter extends BaseAdapter {
    private RecommendSongListDatas.ContentEntity contentEntities;
    private Context context;
    private List<RecommendSongListDatas.ContentEntity.ListEntity> lists;
    private ImageLoader imageLoader;


    public RecommendSongListAdapter(Context context,RecommendSongListDatas.ContentEntity con) {
        this.context = context;
        this.contentEntities = con;
        lists = con.getList();
        imageLoader = VolleySingleton.getInstance(context).getImageLoader();
    }

    @Override
    public int getCount() {

        return 6;
    }

    @Override
    public Object getItem(int position) {
        return contentEntities != null && lists.size() > 0 ? lists.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_recom_gridview,null);
            holder = new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.songImage);
            holder.textView = (TextView) convertView.findViewById(R.id.hostText);
            holder.hostBottomText = (TextView) convertView.findViewById(R.id.hostbotoomText);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        //根据下标取数据
        RecommendSongListDatas.ContentEntity.ListEntity contentEntity = (RecommendSongListDatas.ContentEntity.ListEntity)
                getItem(position);
        holder.textView.setText(contentEntity.getListenum());
        holder.hostBottomText.setText(contentEntity.getTitle());
        String url = contentEntity.getPic();
        //Picasso.with(context).load(url).into(holder.imageView);
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.imageView,
                R.mipmap.default_app_rec, R.mipmap.default_app_rec);
        imageLoader.get(url,listener);
        return convertView;
    }

    public class ViewHolder{
        private ImageView imageView;
        private TextView textView,hostBottomText;
    }
}

